<script lang="ts" setup>
import {ref, computed} from 'vue'
import {useMouseInElement} from '@vueuse/core'

/*获取父组件的传值*/
defineProps<{
    images: string[]
}>()
// 当前显示的图片索引
let active = ref(0)
// ref 获取 DOM 元素的位置
const target = ref(null)
// isisOutside为 true 的时候代表鼠标未进入目标元素，为 false 时代表鼠标进入目标元素
const {elementX, elementY, isOutside} = useMouseInElement(target)
// 遮罩半透明图在商品大图中的坐标位置
const position = computed(() => {
    let x = elementX.value - 70
    let y = elementY.value - 70
    if (x <= 0) x = 0
    if (x >= 140) x = 140
    if (y <= 0) y = 0
    if (y >= 140) y = 140
    return {x, y}
})
</script>
<template>
    <div class="product-image">
        <!-- 放大 -->
        <div class="large" :style="[{ backgroundImage: `url(${images[active]})`,
          backgroundPosition: `-${position.x * 1}px -${position.y * 1}px`
        }]" v-show="!isOutside"></div>
        <div ref="target" class="middle">
            <img :src="images[active]" alt=""/>
            <!-- 遮罩层 -->
            <div class="layer" v-show="!isOutside" :style="{ left: `${position.x}px`, top: `${position.y}px` }"></div>
        </div>
        <ul class="small">
			<li class="arrow_left">
				<el-button >
				    <el-icon><ArrowLeftBold /></el-icon> 
				</el-button>
			</li>
            <li v-for="(item, index) in images" :key="item"
                    :class="{ active: index === active }"
                    @mouseenter="active = index">
                <img :src="item" alt=""/>
            </li>
			<li class="arrow_right">
				<el-button >
				   <el-icon><ArrowRightBold /></el-icon>
				</el-button>
			</li>
        </ul>
    </div>
</template>

<style lang="scss" scoped>
	.arrow_left{
		width: 30px !important;
		box-sizing: border-box;
		padding-right: 6px;
		height: 70px;
		margin: 0px;
		border-radius: 5px 0 0 5px;
	}
	.arrow_right{
		width: 30px !important;
		box-sizing: border-box;
		padding-left: 6px;
		height: 70px;
		margin: 0px;
		border-radius: 0 5px 5px 0;
	}
	.arrow_left .el-button, .arrow_right .el-button{
		height: 70px;
		box-sizing: border-box;
		padding: 20px 4px;
		background: #e8e9ea;
		border: none;
	}
.product-image {
  position: relative;
  z-index: 500;
  .large {
    position: absolute;
    top: 0;
    left: 480px;
    width: 500px;
    height: 500px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 156% 156%;
    background-color: #f8f8f8;
  }
  .middle {
    width: 480px;
    height: 480px;
	border-radius: 10px;
	overflow: hidden;
    background: #f5f5f5;
    position: relative;
    cursor: move;
    .layer {
      width: 140px;
      height: 140px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
    img{
      width: 480px;
      height: 480px;
    }
  }
  .small {
    width: 480px;
    display: flex;
    margin: 10px 0 0 0;
	padding: 0px;
    li {
      width: 70px;
      height: 70px;
      cursor: pointer;
      list-style: none;
      img{
        width: 70px;
        height: 70px;
      }
    }
  }
}
</style>

